package ge.telasi.tasks.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JWindow;

/**
 * @author dimitri
 */
public class NotificationWindow extends JWindow {
    private static final long serialVersionUID = 2003322398417448812L;
    static final int SHOW_NOTIFICATION_FOR = 60; // sec
    private JLabel lblTitle;
    private JTextArea txtMessage;
    private long closeAt;

    public NotificationWindow() {
        setSize(new Dimension(200, 150));
        getContentPane().setLayout(new BorderLayout());
        JComponent mainComp = createMainComponent();
        getContentPane().add(mainComp, BorderLayout.CENTER);
        mainComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
    }

    public synchronized void display() {
        if (!isVisible()) {
            Runnable run = new Runnable() {
                public void run() {
                    openWindow();
                    closeAt = System.currentTimeMillis() + SHOW_NOTIFICATION_FOR * 1000;
                    while(true) {
                        long current = System.currentTimeMillis();
                        try {
                            Thread.sleep(500);
                        } catch (Exception ex) {}
                        if (current >= closeAt) {
                            break;
                        }
                    }
                    setVisible(false);
                }
            };
            Thread t = new Thread(run);
            t.start();
        }
    }

    synchronized void openWindow() {
        GraphicsEnvironment graphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle desktop=graphicsEnvironment.getMaximumWindowBounds();
        Dimension screen = desktop.getSize();
        Dimension size = getSize();
        int x1 = screen.width - size.width;
        int y1 = screen.height  - size.height;
        setVisible(true);
        setAlwaysOnTop(true);
        setLocation(x1, y1);
    }

    public void addMessage(String text) {
        display();
        Message msg = new Message(text);
        SimpleDateFormat frmt = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        txtMessage.setText(msg.getText());
        txtMessage.setCaretPosition(0);
        lblTitle.setText(frmt.format(msg.getDate()));
        Toolkit.getDefaultToolkit().beep();
    }

    private JComponent createMainComponent() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(createTitleComponent(), BorderLayout.NORTH);
        panel.add(createTextMessage(), BorderLayout.CENTER);
        return panel;
    }

    private JComponent createTitleComponent() {
        JPanel panel = new JPanel(new BorderLayout());
        lblTitle = new JLabel(UIUtils.getIcon16x16("logo.png"));
        lblTitle.setText("<title>");
        panel.add(lblTitle, BorderLayout.WEST);
        JButton btnClose = new JButton(UIUtils.getIcon16x16("cross.png"));
        btnClose.setToolTipText("დახურვა");
        btnClose.setPreferredSize(new Dimension(25, 25));
        btnClose.setBorderPainted(false);
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        panel.add(btnClose, BorderLayout.EAST);
        panel.setBackground(Color.GRAY);
        panel.setOpaque(true);
        return panel;
    }

    private JComponent createTextMessage() {
        txtMessage = new JTextArea();
        txtMessage.setEditable(false);
        txtMessage.setWrapStyleWord(true);
        txtMessage.setLineWrap(true);
        return new JScrollPane(txtMessage);
    }

    private class Message {
        private String text;
        private Date date;

        Message(String text) {
            this.text = text;
            this.date = new Date();
        }

        public String getText() {
            return text;
        }

        public Date getDate() {
            return date;
        }
    }

}
